/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.servlet.admin;

import cn.eclass.bean.api.ISchoolBean;
import cn.eclass.bean.api.ISchoolOrderBean;
import cn.eclass.bean.api.IUserBean;
import cn.eclass.entity.SchoolEntity;
import cn.eclass.entity.SchoolOrderEntity;
import cn.eclass.entity.UserEntity;
import cn.eclass.servlet.ActionFace;
import cn.eclass.utils.BeanFactory;
import cn.eclass.utils.ConstantsConf;
import cn.eclass.utils.Helper;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;

/**
 *
 * @author fortruth
 */
public class Schoolorder extends ActionFace {

    ISchoolOrderBean bean = BeanFactory.factory.getSchoolOrderBean();

    @Override
    public void doIndex() {
        try {
            List<SchoolOrderEntity> entities = bean.list();

            Field[] fields = SchoolOrderEntity.class.getDeclaredFields();
            List<HashMap<String, String>> theFields = Helper.buildField(fields, module);

            request.setAttribute("entities", entities);
            request.setAttribute("fields", theFields);
            request.getRequestDispatcher(adminView).forward(request, response);
        } catch (ServletException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void doCreate() {
        SchoolOrderEntity entity = new SchoolOrderEntity();
        bean.add(form2Entity(entity));
        this.doIndex();
    }

    public void doConfirm() {
        int id = Integer.valueOf(request.getParameter("id"));
        float amount = Float.valueOf(request.getParameter("amount"));
        String comment = request.getParameter("comment");
        int points = (int) Math.ceil(amount / ConstantsConf.ratio);
        //update schoolorder status
        SchoolOrderEntity entity = bean.get(id);
        entity.setValid(1);
        entity.setAmount(amount);
        entity.setPoints(points);
        entity.setComment(comment);
        bean.update(entity);
        //update school amount;
        ISchoolBean schoolBean = beanFactory.getSchoolBean();
        SchoolEntity school = schoolBean.get(entity.getSchoolId());
        school.setOrderAmount(school.getOrderAmount() + 1);
        schoolBean.update(school);
        
        //update schoollink amount;
        
        //update user points
        IUserBean userBean = beanFactory.getUserBean();
        UserEntity user = userBean.get(entity.getUserId());
        user.setPoints(user.getPoints() + points);
        userBean.update(user);
        //update points
        this.doIndex();
    }

    /*
     * ajax action
     */
    public void doSelect() {
        int id = Integer.valueOf(request.getParameter("id"));
        SchoolOrderEntity entity = bean.get(id);
        out.print(new Gson().toJson(entity));
    }

    public void doUpdate() {
        int id = Integer.valueOf(request.getParameter("id"));
        SchoolOrderEntity entity = bean.get(id);
        bean.update(form2Entity(entity));
        this.doIndex();
    }

    public void doDelete() {
        int id = Integer.valueOf(request.getParameter("id"));
        bean.delete(id);
        this.doIndex();
    }

    private SchoolOrderEntity form2Entity(SchoolOrderEntity entity) {
        entity.setAmount(Float.valueOf(request.getParameter("amount")));
        entity.setSchoolId(Integer.valueOf(request.getParameter("schoolId")));
        entity.setValid(Integer.valueOf(request.getParameter("valid")));
        entity.setUserId(Integer.valueOf(request.getParameter("userId")));
        entity.setComment(request.getParameter("comment"));
        return entity;
    }
}
